import java.io.File;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


//asdfasdasdfasdfsmmmmm
public class ss {
	public static void main(String[] args) {
	    String host = "https://dm-51.data.aliyun.com";
	    String path = "/rest/160601/ocr/ocr_idcard.json";
	    String method = "POST";
	    String appcode = "56dd39b4655a4f21a084bc7df5e8cbf9";
	    Map<String, String> headers = new HashMap<String, String>();
	    String str = Imagebase64.getImageBinary(new File("e:\\test.png"));
	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	    headers.put("Authorization", "APPCODE " + appcode);
	    //根据API的要求，定义相对应的Content-Type
	    headers.put("Content-Type", "application/json; charset=UTF-8");
	    Map<String, String> querys = new HashMap<String, String>();
	    String bodys = "{" + "\"inputs\": [" +
	    "{" + "\"image\": {" + "\"dataType\": 50," + "\"dataValue\": \"%s\""
	    		+ "}," + "\"configure\": {" + "\"dataType\": 50,"+
	    		"\"dataValue\": \"{" + "\\" + "\"side"+"\\"+"\": "+"\\"+"\"face"+"\\"+"\""+
	    		"}\"}}]}";
	    try {
	    	/**
	    	* 重要提示如下:
	    	* HttpUtils请从
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
	    	* 下载
	    	*
	    	* 相应的依赖请参照
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
	    	*/
	    	bodys = String.format(bodys, str);
	    	HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
	    
 	
	    	String jsonStr = EntityUtils.toString(response.getEntity());
	    	//dataValue
	    	int ii = jsonStr.lastIndexOf("dataValue");
	    	String ss = jsonStr.substring(ii + 11);
	    	int idx = ss.indexOf("\"");
	    	ss = ss.substring(idx + 1,ss.length() - 5).replace("\\", "");
	    	idx = ss.indexOf("name");
	    	ss = "{\"" +ss.substring(idx,ss.length());
	    	System.out.println(ss);
	    	Gson gson = new Gson();
	    	Type type = new TypeToken<IdCardModel>() {  
	        }.getType(); 
	    	IdCardModel mm = gson.fromJson(ss, type);
	    	System.out.println(mm.getName() + ":" + mm.getNum());
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	}	
	
	/***
     * json字符串转java List
     * @param rsContent
     * @return
     * @throws Exception
     */
    
}
